/*
 * This file is part of aion-engine <aion-engine.com>
 *
 * aion-engine is private software: you can redistribute it and or modify
 * it under the terms of the GNU Lesser Public License as published by
 * the Private Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * aion-engine is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser Public License for more details.
 *
 * You should have received a copy of the GNU Lesser Public License
 * along with aion-engine.  If not, see <http://www.gnu.org/licenses/>.
 */
package com.aionengine.gameserver.network.aion.serverpackets;

import com.aionengine.gameserver.model.gameobjects.HouseDecoration;
import com.aionengine.gameserver.model.gameobjects.HouseObject;
import com.aionengine.gameserver.model.gameobjects.UseableItemObject;
import com.aionengine.gameserver.model.gameobjects.player.Player;
import com.aionengine.gameserver.network.aion.AionConnection;
import com.aionengine.gameserver.network.aion.AionServerPacket;

/**
 * @author Rolandas
 */
public class SM_HOUSE_REGISTRY extends AionServerPacket {

    int action;

    public SM_HOUSE_REGISTRY(int action) {
        this.action = action;
    }

    @Override
    protected void writeImpl(AionConnection con) {
        Player player = con.getActivePlayer();
        if (player == null)
            return;

        writeC(action);
        if (action == 1) { // Display registered objects
            if (player.getHouseRegistry() == null) {
                writeH(0);
                return;
            }
            writeH(player.getHouseRegistry().getNotSpawnedObjects().size());
            for (HouseObject<?> obj : player.getHouseRegistry().getNotSpawnedObjects()) {
                writeD(obj.getObjectId());
                int templateId = obj.getObjectTemplate().getTemplateId();
                writeD(templateId);
                writeD(player.getHouseObjectCooldownList().getReuseDelay(obj.getObjectId()));
                if (obj.getUseSecondsLeft() > 0)
                    writeD(obj.getUseSecondsLeft());
                else
                    writeD(0);

                Integer color = null;
                if (obj != null)
                    color = obj.getColor();

                if (color != null && color > 0) {
                    writeC(1); // Is dyed (True)
                    writeC((color & 0xFF0000) >> 16);
                    writeC((color & 0xFF00) >> 8);
                    writeC((color & 0xFF));
                } else {
                    writeC(0); // Is dyed (False)
                    writeC(0);
                    writeC(0);
                    writeC(0);
                }
                writeD(0); // expiration as for armor ?

                writeC(obj.getObjectTemplate().getTypeId());
                if (obj instanceof UseableItemObject) {
                    ((UseableItemObject) obj).writeUsageData(getBuf());
                }
            }
        } else if (action == 2) { // Display default and registered decoration items
            writeH(player.getHouseRegistry().getDefaultParts().size() + player.getHouseRegistry().getCustomParts().size());
            for (HouseDecoration deco : player.getHouseRegistry().getDefaultParts()) {
                writeD(0);
                writeD(deco.getTemplate().getId());
            }
            for (HouseDecoration houseDecor : player.getHouseRegistry().getCustomParts()) {
                writeD(houseDecor.getObjectId());
                writeD(houseDecor.getTemplate().getId());
            }
        }
    }
}
